﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using ShopDAL.Models;

namespace NT40Redis1.Controllers
{
    public class ProductsController : Controller
    {
        RedisCache redis;
        IConfiguration _config;
        public ProductsController(IConfiguration config,RedisCache cache)
        {
            redis = cache;
            _config = config;
        }
        // GET: Products
        public  async Task<IActionResult> Index()
        {

            //使用redis
            //1。从缓存中读取数据
            string prodJson = redis.GetString("prods");
            if (string.IsNullOrEmpty(prodJson))
            {
                //缓存中没有数据
                HttpClient httpClient = new HttpClient();

                prodJson = await httpClient.GetStringAsync(_config.GetSection("APIURL").Value);
                redis.SetString("prods", prodJson);
            }
            IEnumerable<Products> prodList = JsonConvert.DeserializeObject<IEnumerable<Products>>(prodJson);
            return View(prodList);
        }

        // GET: Products/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Products/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Products/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: Products/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Products/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: Products/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Products/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}